package com.supermarket.servlet;

import com.supermarket.entity.Bill;
import com.supermarket.entity.Provider;
import com.supermarket.service.Adminservice;
import com.supermarket.service.ProviderService;
import com.supermarket.service.lmpl.AdminserviceImpl;
import com.supermarket.service.lmpl.ProviderServiceImpl;
import com.supermarket.util.currentPage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ProviderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter ( "action" );
        if (action.equals ( "show" )) {
            this.show ( req, resp );

        } else if (action.equals ( "fenye" )) {
            this.fenye ( req, resp );
        } else if (action.equals ( "search" )) {
            this.getsearch ( req, resp );
        }else if(action.equals ( "del" )){
            this.delete(req,resp);

        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter ( "id" );
        int i = Integer.parseInt ( id );
        ProviderService service = new ProviderServiceImpl ();
        int aa=service.servicedel(i);
        if (aa==0){
            try {
                resp.sendRedirect ( "jsp/del.jsp" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }else if (aa==1){
            try {
                resp.sendRedirect ( "/ProviderServlet?action=fenye" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }


    }

    private void getsearch(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter ( "id" );
        ProviderService service = new ProviderServiceImpl ();
        Provider provider = service.selectid ( Integer.valueOf ( id ) );
        req.setAttribute ( "providers", provider );
        try {
            req.getRequestDispatcher ( "/jsp/proivder_detail.jsp" ).forward ( req, resp );
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }

    }

    private void fenye(HttpServletRequest req, HttpServletResponse resp) {

        String parameter = req.getParameter ( "pageIndex" );
        String name = req.getParameter ( "providerName" );//供应商
        String type = req.getParameter ( "providerDesc" );//供应商描述
        int pageIndex = 1;//这里表示初始化为1；
        int pagesize = 2;//表是每页有2条数据

        //第一次这个东西是没有数据的
        if (parameter != null && (!parameter.equals ( "" ))) {
            //这里表示有数据才可以转换
            pageIndex = Integer.valueOf ( parameter );
        }

        ProviderService adminservice = new ProviderServiceImpl ();
        int count = adminservice.count ( name, type );
        currentPage page = new currentPage ();
        page.setSetTotalCount ( count );
        page.setTotalPageCount ( (count % pagesize) == 0 ? count / pagesize : (count / pagesize) + 1 );
        page.setPageSize ( pagesize );
        page.setCurrentPage ( pageIndex );
        List<Provider> providers = adminservice.dd ( pageIndex, pagesize, name, type );
        req.setAttribute ( "providers", providers );
        req.setAttribute ( "page", page );
        if (name != null && !name.equals ( "" )) {
            req.setAttribute ( "mingcheng", name );
        }
        if (type != null && !type.equals ( "" )) {
            req.setAttribute ( "miaoshu", type );
        }
        try {
            req.getRequestDispatcher ( "/jsp/admin_provider_list.jsp" ).forward ( req, resp );
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter ( "action" );
        if (action.equals ( "fenye" )) {
            this.fenye ( req, resp );
        } else if (action.equals ( "update" )) {
            this.getupdate ( req, resp );
        }else if (action.equals ( "add" )){
            this.getadd(req, resp );
        }
    }

    private void getadd(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter ( "id" );
        String name = req.getParameter ( "name" );
        String description = req.getParameter ( "description" );
        String contact = req.getParameter ( "contact" );
        String phone = req.getParameter ( "phone" );
        String address = req.getParameter ( "address" );
        String fax = req.getParameter ( "fax" );
        Provider provider=new Provider (Integer.valueOf (id),name,description,contact,phone,address,fax);
        ProviderService service=new ProviderServiceImpl ();
        int a=service.add(provider);
        if (a==1){
            try {
                resp.sendRedirect ( "/ProviderServlet?action=fenye" );
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }

    private void getupdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter ( "id" );
        String name = req.getParameter ( "name" );
        String description = req.getParameter ( "description" );
        String contact = req.getParameter ( "contact" );
        String phone = req.getParameter ( "phone" );
        String address = req.getParameter ( "address" );
        String fax = req.getParameter ( "fax" );
        Provider provider=new Provider (Integer.valueOf (id),name,description,contact,phone,address,fax);
        ProviderService service=new ProviderServiceImpl ();
        int a=service.update(provider);
        if (a==1){
            resp.sendRedirect ( "/ProviderServlet?action=fenye" );
        }

    }


    private void show(HttpServletRequest req, HttpServletResponse resp) {
        Adminservice adminservice = new AdminserviceImpl ();
        List<Provider> provider = adminservice.getProvider ();

        req.setAttribute ( "providers", provider );
        try {
            req.getRequestDispatcher ( "jsp/admin_provider_add.jsp" ).forward ( req, resp );
        } catch (ServletException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}
